package com.njtoyo.taxi.admin.library.taxi_service;

import com.njtoyo.taxi.admin.library.taxi_service.dto.SimpleCallSendAppointment;
import com.njtoyo.taxi.admin.mapper.master.entity.SimpleCallBacklog;
import com.njtoyo.taxi.admin.mapper.master.entity.SimpleCallRide;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Slf4j
@Service
public class SimpleCallTaxiService {

    @Autowired
    private RestTemplate restTemplate;

    @Value("${config.taxiService.simpleCallUrl}")
    private String baseUrl;

    public Long send(Map params) {
        String url = baseUrl + "/simple_call/call/administrators";
        log.info("电召发单接口: {}, {}", url, params);
        try {
            SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
            factory.setConnectTimeout(6000);
            factory.setReadTimeout(2000);

            RestTemplate restTemplate = new RestTemplate(factory);
            SimpleCallSendResponse res = restTemplate.postForObject(url, params, SimpleCallSendResponse.class);
            log.info("接口响应信息: {}", res);
            if (Objects.nonNull(res) && res.getCode() == 0) {
                Map data = (Map) res.getData();
                if (Objects.nonNull(data.get("callId"))) {
                    return Long.valueOf(data.get("callId").toString());
                }
            }
        } catch (RestClientException e) {
            log.info("电召发单请求失败：", e);
        }
        return null;
    }

    @Async
    public void sendCancelMsg(SimpleCallBacklog backlog) {
        String url = baseUrl + "/simple_call/general/send/sms";
        HashMap<Object, Object> request = new HashMap<>();
        request.put("mobileNumber", backlog.getMobileNumber());
        request.put("adCode", backlog.getAdCode());

        log.info("电召取消发送短信接口, {}, {}", url, request);
        try {
            TaxiServiceResponse res = restTemplate.postForObject(url, request, TaxiServiceResponse.class);
            log.info("电召取消发送短信接口返回：{}", res);
        } catch (RestClientException e) {
            log.info("电召取消发送短信接口，请求外部接口失败: {}", e.getMessage());
        }
    }


    @Async
    public void sendMsg(String mobileNumber, String templateCode, String templateParams) {
        String url = baseUrl + "/simple_call/open/admin/sms";
        HashMap<Object, Object> request = new HashMap<>();
        request.put("mobileNumber", mobileNumber);
        request.put("templateCode", templateCode);
        request.put("templateParams", templateParams);

        log.info("电召发送短信接口, {}, {}", url, request);
        try {
            TaxiServiceResponse res = restTemplate.postForObject(url, request, TaxiServiceResponse.class);
            log.info("电召发送短信接口返回：{}", res);
        } catch (RestClientException e) {
            log.info("电召发送短信接口，请求外部接口失败: {}", e.getMessage());
        }
    }

    @Async
    public void sendRideStateMqtt(SimpleCallRide ride) {
        String url = baseUrl + "/simple_call/general/send/ride/mqtt";
        HashMap<Object, Object> request = new HashMap<>();
        request.put("simpleCallRideId", ride.getId());

        log.info("修改行程状态Mqtt通知接口, {}, {}", url, request);
        try {
            TaxiServiceResponse res = restTemplate.postForObject(url, request, TaxiServiceResponse.class);
            log.info("修改行程状态Mqtt通知接口返回：{}", res);
        } catch (RestClientException e) {
            log.info("修改行程状态Mqtt通知接口，请求外部接口失败: {}", e.getMessage());
        }
    }

    @Async
    public void sendCancelCallMqtt(Long simpleCallId,Boolean isCancel) {
        String url = baseUrl + "/simple_call/general/send/call/mqtt";
        HashMap<Object, Object> request = new HashMap<>();
        request.put("simpleCallId", simpleCallId);
        request.put("isCancel", isCancel);
        log.info("取消一键叫车呼叫单状态Mqtt通知接口, {}, {}", url, request);
        try {
            TaxiServiceResponse res = restTemplate.postForObject(url, request, TaxiServiceResponse.class);
            log.info("取消一键叫车呼叫单状态Mqtt通知接口返回：{}", res);
        } catch (RestClientException e) {
            log.info("取消一键叫车呼叫单状态Mqtt通知接口，请求外部接口失败: {}", e.getMessage());
        }
    }


    public Long sendOnCall(Long id) {
        String url = baseUrl + "/simple_call/call/best_tone";
        Map<String, Object> params = new HashMap<>();
        params.put("onCallBackLogId", id);
        log.info("on-call发单接口: {}, {}", url, params);
        try {
            SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
            factory.setConnectTimeout(6000);
            factory.setReadTimeout(2000);

            RestTemplate restTemplate = new RestTemplate(factory);
            SimpleCallSendResponse res = restTemplate.postForObject(url, params, SimpleCallSendResponse.class);
            log.info("on-call接口响应信息: {}", res);
            if (Objects.nonNull(res) && res.getCode() == 0) {
                Map data = (Map) res.getData();
                if (Objects.nonNull(data.get("callId"))) {
                    return Long.valueOf(data.get("callId").toString());
                }
            }
        } catch (RestClientException e) {
            log.info("on-call发单请求失败：", e);
        }
        return null;
    }


    public Long sendAppointment(SimpleCallSendAppointment sendAppointmentParams) {
        String url = baseUrl + "/simple_call/call/administrators/appointment";
        log.info("电召发单预约单接口: {}, {}", url, sendAppointmentParams);
        try {
            SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
            factory.setConnectTimeout(6000);
            factory.setReadTimeout(2000);

            RestTemplate restTemplate = new RestTemplate(factory);
            SimpleCallSendResponse res = restTemplate.postForObject(url, sendAppointmentParams, SimpleCallSendResponse.class);
            log.info("接口响应信息: {}", res);
            if (Objects.nonNull(res) && res.getCode() == 0) {
                Map data = (Map) res.getData();
                if (Objects.nonNull(data.get("callId"))) {
                    return Long.valueOf(data.get("callId").toString());
                }
            }
        } catch (RestClientException e) {
            log.info("电召发单预约单接口：", e);
        }
        return null;
    }

}
